import Vue from 'vue'
import Vuex from 'vuex'
// ----------------
import wzStore from './modules/wzStore'
import zlStore from './modules/zlStore'
import hpStore from './modules/hpStore'
import cjStore from './modules/cjStore'
import ljnStore from './modules/ljnStore'//注释
import bsStore from './modules/bsStore'
import hxcStore from './modules/hxcStore'
import * as mySession from '../http/public'
// -----------
import * as api from '../http/api'
import axios from '../http/http'
// -----------
Vue.use(Vuex)
export default new Vuex.Store({
  modules: {
    wzStore,
    hpStore,
    zlStore,
    ljnStore,
    bsStore,
    hxcStore,
    cjStore
  },
  state: {
    user: {
      name: sessionStorage.getItem('userName') || undefined,
      type: sessionStorage.getItem('type') || undefined,
      token: sessionStorage.getItem('token') || undefined
    },
    admin: {
      setCp: true,
      setYy: true,
      setKc: true,
      setJc: true,
      setXt: false,
      setCw: false
    },
    sAdmin: {
      setCp: true,
      setYy: true,
      setKc: true,
      setJc: true,
      setXt: true,
      setCw: true
    },
    account: {
      setCp: false,
      setYy: false,
      setKc: false,
      setJc: false,
      setXt: false,
      setCw: true
    },
    navQx: {}
  },
  actions: {
    loginAct({ commit }, obj) { //登录
      // console.log(obj)
      return axios({
        url: 'mgrsite/Login/login',
        data: obj,
        method: 'post'
      }).then(res => {
        // console.log(res)
        sessionStorage.setItem('userName', res.data.name)
        sessionStorage.setItem('type', res.data.roleId)
        commit('addUser', res.data)
        return res.data.code
      })
    },
    outLoginAct({ commit }) { //退出登录
      sessionStorage.removeItem('userName')
      sessionStorage.removeItem('type')
      sessionStorage.removeItem('token')
      commit('loginOut')
    },
    getQx({ commit }) { //初始化权限
      commit("upNavQx")
    },
    bjQxAct({ commit }, name) {
      commit("bjQx", name)
    }
  },
  mutations: {
    addUser(state, obj) { //登录
      state.user.name = obj.name
      state.user.type = obj.roleId
      state.user.token = obj.Authorization
    },
    loginOut(state) { //退出登录
      state.user = {}
    },
    upNavQx(state) {
      if (!sessionStorage.getItem("account")) {
        mySession.setSession("account", state.account);
      }
      if (!sessionStorage.getItem("admin")) {
        mySession.setSession("admin", state.admin);
      }
      if (!sessionStorage.getItem("sAdmin")) {
        mySession.setSession("sAdmin", state.sAdmin);
      }
      if (state.user.type == 2) {
        state.navQx = mySession.getSession("account");
      } else if (state.user.type == 3) {
        state.navQx = mySession.getSession("admin");
      } else if (state.user.type == 4) {
        state.navQx = mySession.getSession("sAdmin");
      }
    },
    bjQx(state, name) {
      // if (state.user.type == 2) {
      //   state.navQx = mySession.getSession(name)
      // } else if (state.user.type == 3) {
      //   state.navQx = mySession.getSession(name)
      // } else if (state.user.type == 4) {
      //   state.navQx = mySession.getSession(name)
      // }
      if (name == "account") {
        state.account = mySession.getSession('account')
        state.navQx = mySession.getSession('account')
      } else if (name == "admin") {
        state.admin = mySession.getSession('admin')
        state.navQx = mySession.getSession('admin')
      } else if (name == 'sAdmin') {
        state.sAdmin = mySession.getSession('sAdmin')
        state.navQx = mySession.getSession('sAdmin')
      }
    }
  },
  getters: {
    getUserName(state) {
      return state.user.name
    },
    getUserType(state) {
      return state.user.type
    }
  }
})